<?php
if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

//------------------------------------------------------ -----------------

function get_caidi_qu_list($search_inf=array(),$page=1,$page_num=5,$all=1){//读取菜地内的区表列表，
	

	$res=array();
	if((!is_numeric($page))||$page<1) {
		$page=1;
	}
	if((!is_numeric($page_num))||$page_num<1) {
		$page_num=5;
	}
	$sql ="";
	$wh=" where 1 ";
	if(!empty($search_inf['caidi_id'])){$wh.=" and caidi_id='".$search_inf['caidi_id']."'";}
	
	$sql = "select count(qu_id) as num from " .$GLOBALS['zc']->table('caidi_qu').$wh;       
	
	$num_db=$GLOBALS['db']->getOne($sql);//总记录数
	$page_db=intval($num_db/$page_num)+(fmod($num_db,$page_num)==0?0:1);//总页数
	if($page>$page_db){$page=$page_db;}//如果要显示的页数超过总页数，

	$limit_f=(($page-1)*$page_num)<0?0:($page-1)*$page_num;
	$limit=" limit ".$limit_f.",".$page_num;
	if($all==1){$limit="";}
	$sql ="select * from  ".$GLOBALS['zc']->table('caidi_qu').$wh." order by qu_id desc ".$limit;
	
    $res=$GLOBALS['db']->getAll($sql);
    //foreach ($res as $key=>$val){
    //	$res2[$val['obj_id']][]=$val;
    //}
	return $res;
}

function add_caidi_qu($inf=array(),$inf_arr=array()){//添加菜地内的区表
	
	if(empty($inf)&&empty($inf_arr)){return false;}

	if(!empty($inf)){
		unset($inf['qu_id']);
		$GLOBALS['db']->autoExecute($GLOBALS['zc']->table('caidi_qu'), $inf, 'INSERT');
		$id=mysql_insert_id();
		return $id;
	}else{
		if(!empty($inf_arr)){
			$GLOBALS['db']->autoExecute($GLOBALS['zc']->table('caidi_qu'), $inf_arr, 'INSERT','','',true);
			return true;
		}
	}
   
}
function edt_caidi_qu($inf=array(),$inf_arr=array()){//编辑菜地内的区表
	

	if(!empty($inf)){
		$inf_ok=$inf;
		unset($inf_ok['qu_id']);
		$GLOBALS['db']->autoExecute($GLOBALS['zc']->table('caidi_qu'), $inf_ok, 'UPDATE', 'qu_id = ' . $inf['qu_id']);
		return true;
	}else{
		if(!empty($inf_arr)){//更新记录的条件是$inf_arr[i]['where']
			//print_r($inf_arr);
			$GLOBALS['db']->autoExecute($GLOBALS['zc']->table('caidi_qu'), $inf_arr, 'UPDATE','','',true);
			return true;
		}
	}
	
}

function del_caidi_qu($qu_id='',$caidi_id=''){//删除菜地内的区表
	
	if(empty($qu_id)&&empty($caidi_id)){return false;}
	$wh="";
	if(!empty($qu_id)){$wh=" where qu_id='$qu_id'";}
    if(!empty($caidi_id)){$wh=" where caidi_id='$caidi_id'";}
	$GLOBALS['db']->query("delete from ".$GLOBALS['zc']->table('caidi_qu').$wh);
    return true;	
}

function get_caidi_qu($qu_id){//读取菜地内的区表
	
	$res=array();
	if(empty($qu_id)){return $res;}
	
	$res=$GLOBALS['db']->getRow("select * from ".$GLOBALS['zc']->table('caidi_qu')."  where qu_id='$qu_id'");
	return $res;	
}
//------------------------------------------
function insert_caidi_qu($caidi_id){//增加菜地时插入8个区
	if(empty($caidi_id)){return false;}
	$caidi_qu_arr=array();
	//del_caidi_qu('',$caidi_id);//删除之前的菜地id
	$caidi_qu_list=get_caidi_qu_list(array("caidi_id"=>$caidi_id));
	$qu_db_num=count($caidi_qu_list);//已有区的数量
	if($qu_db_num>CAIDI_QU_NUN){
		if(!empty($caidi_qu_list)){//删除比设置还多余的区
			$i=1;
			foreach ($caidi_qu_list as $key=>$val){
				if($i>CAIDI_QU_NUN){del_caidi_qu($val['qu_id']);}
			}
			$i++;
		}
	}elseif($qu_db_num<CAIDI_QU_NUN){
		for($i=1;$i<=(CAIDI_QU_NUN-$qu_db_num);$i++){
			$caidi_qu_arr[]=array("caidi_id"=>$caidi_id,"qu_name"=>$i."区","grow_inf"=>"新地块，待耕种","act_inf"=>"未发起指令");
		}
	}
	if(!empty($caidi_qu_arr)){
		add_caidi_qu('',$caidi_qu_arr);
		return true;
	}
}

function delete_caidi_qu($caidi_id){//增加菜地时插入8个区
	if(empty($caidi_id)){return false;}
	$caidi_qu_arr=array();
	del_caidi_qu('',$caidi_id);//删除之前的菜地id
	return true;
}

function print_pre_r($cat_list){
	print ( '<pre>' )  ;
	print_r ( $cat_list ) ;
	print ( '</pre>' ) ;
}
//------------------------------------------
function get_caidi_lease_list($search_inf=array(),$page=1,$page_num=5,$all=1){//读取客户租回来的菜地表列表，
	

	$res=array();
	if((!is_numeric($page))||$page<1) {
		$page=1;
	}
	if((!is_numeric($page_num))||$page_num<1) {
		$page_num=5;
	}
	$sql ="";
	$wh=" where 1 ";
	$join="";
	if(!empty($search_inf['id_in'])){$wh.=" and id in(".$search_inf['id_in'].")";}
	if(!empty($search_inf['goods_id'])){$wh.=" and goods_id='".$search_inf['goods_id']."'";}
	if(!empty($search_inf['user_id'])){$wh.=" and user_id='".$search_inf['user_id']."'";}
	if(!empty($search_inf['lease_status'])){$wh.=" and lease_status='".$search_inf['lease_status']."'";}
	if(!empty($search_inf['cat_id'])){
		$search_inf['more_inf']=1;
		$wh.=" and cat_id='".$search_inf['cat_id']."'";
	}
	if($search_inf['more_inf']){$join.="  as cd left join (select goods_id as g_id,goods_name,cat_id from ".$GLOBALS['ecs']->table('goods').") as gd on cd.goods_id=gd.g_id ";}
	
	$sql = "select count(id) as num from " .$GLOBALS['zc']->table('caidi_lease').$join.$wh;       
	
	$num_db=$GLOBALS['db']->getOne($sql);//总记录数
	$page_db=intval($num_db/$page_num)+(fmod($num_db,$page_num)==0?0:1);//总页数
	if($page>$page_db){$page=$page_db;}//如果要显示的页数超过总页数，

	$limit_f=(($page-1)*$page_num)<0?0:($page-1)*$page_num;
	$limit=" limit ".$limit_f.",".$page_num;
	if($all==1){$limit="";}
	$sql ="select * from  ".$GLOBALS['zc']->table('caidi_lease').$join.$wh." order by id desc ".$limit;
	
    $res=$GLOBALS['db']->getAll($sql);
    foreach ($res as $key=>$val){
    	$res2[$val['goods_id']]=$val;
    }
	return $res2;
}
function get_caidi_lease($id){//读取用户租用的资产
	
	$res=array();
	if(empty($id)){return $res;}
	
	$res=$GLOBALS['db']->getRow("select * from ".$GLOBALS['zc']->table('caidi_lease')."  where id='$id'");
	return $res;	
}
//------------------------------------------
function get_message_goods_list($search_inf=array(),$page=1,$page_num=5,$all=1){//读取资产管理日志表列表，
	

	$res=array();
	if((!is_numeric($page))||$page<1) {
		$page=1;
	}
	if((!is_numeric($page_num))||$page_num<1) {
		$page_num=5;
	}
	$sql ="";
	$wh=" where 1 ";
	$join="";
	if(!empty($search_inf['qu_id_in'])){$wh.=" and qu_id in(".$search_inf['qu_id_in'].")";}
	if(!empty($search_inf['qu_id'])){$wh.=" and qu_id='".$search_inf['qu_id']."'";}
	if(!empty($search_inf['from_user_id'])){$wh.=" and from_user_id='".$search_inf['from_user_id']."'";}

	$sql = "select count(msg_id) as num from " .$GLOBALS['zc']->table('message_goods').$wh;       
	
	$num_db=$GLOBALS['db']->getOne($sql);//总记录数
	$page_db=intval($num_db/$page_num)+(fmod($num_db,$page_num)==0?0:1);//总页数
	if($page>$page_db){$page=$page_db;}//如果要显示的页数超过总页数，

	$limit_f=(($page-1)*$page_num)<0?0:($page-1)*$page_num;
	$limit=" limit ".$limit_f.",".$page_num;
	if($all==1){$limit="";}
	$sql ="select * from  ".$GLOBALS['zc']->table('message_goods').$wh." order by add_time desc ".$limit;
	
    $res=$GLOBALS['db']->getAll($sql);
    foreach ($res as $key=>$val){
    	$val['add_time_txt']=local_date_zc("Y-m-j H:i", $val['add_time']);
    	switch ($val['from_user_role']){//发送者角色：keeper管家user用户
    		case 'keeper':
    			$val['from_user_role_txt']="管家";
    			break;
    		case 'user':
    			$val['from_user_role_txt']="用户";
    			break;
    	}
    	$res2[$val['msg_id']]=$val;
    }
	return $res2;
}

//------------------------------------------
function upload_img($html_name='',$save_dir='',$save_name=''){//$html_name页面控件名称,$save_dir保存目录，$save_name保存文件名
	include_once(ROOT_PATH . '/includes/cls_image.php');
    $image = new cls_image($_CFG['bgcolor']);
	/* 检查图片：如果有错误，检查尺寸是否超过最大值；否则，检查文件类型 */
    if (isset($_FILES[$html_name]['error'])) // php 4.2 版本才支持 error
    {
        // 最大上传文件大小
        $php_maxsize = ini_get('upload_max_filesize');
        $htm_maxsize = '2M';

        // 上传图片
        if ($_FILES[$html_name]['error'] == 0)
        {
            if (!$image->check_img_type($_FILES[$html_name]['type']))
            {
                sys_msg('错误的图片格式', 1, array(), false);
            }
        }
        elseif ($_FILES[$html_name]['error'] == 1)
        {
            sys_msg(sprintf('上传图片过大!', $php_maxsize), 1, array(), false);
        }
        elseif ($_FILES[$html_name]['error'] == 2)
        {
            sys_msg(sprintf('上传图片过大！', $htm_maxsize), 1, array(), false);
        }
    }
    // 如果上传了图片，相应处理
    if ($_FILES[$html_name]['tmp_name'] != '' && $_FILES[$html_name]['tmp_name'] != 'none')
    {

        $original_img   = $image->upload_image($_FILES[$html_name],$save_dir,$save_name); // 原始图片

        if ($original_img === false)
        {
            sys_msg($image->error_msg(), 1, array(), false);
        }

        // 如果系统支持GD，缩放商品图片，且给商品图片和相册图片加水印
        if ($proc_thumb && $image->gd_version() > 0 && $image->check_img_function($_FILES[$html_name]['type']))
        {

        	// 如果设置大小不为0，缩放图片
        	if ($_CFG['image_width'] != 0 || $_CFG['image_height'] != 0)
        	{
        		$original_img_thumb = $image->make_thumb('../'. $original_img , $GLOBALS['_CFG']['image_width'],  $GLOBALS['_CFG']['image_height']);
        		if ($original_img_thumb === false)
        		{
        			sys_msg($image->error_msg(), 1, array(), false);
        		}
        	}

        	// 加水印
        	if (intval($_CFG['watermark_place']) > 0 && !empty($GLOBALS['_CFG']['watermark']))
        	{
        		if ($image->add_watermark('../'.$original_img,'',$GLOBALS['_CFG']['watermark'], $GLOBALS['_CFG']['watermark_place'], $GLOBALS['_CFG']['watermark_alpha']) === false)
        		{
        			sys_msg($image->error_msg(), 1, array(), false);
        		}
        	}
        }
        $img_name=array_pop(explode('/',$original_img));
        $image_arr['image']=$original_img;
        $image_arr['img_name']=$img_name;
        $image_arr['thumb']=$original_img_thumb;
        $image_arr['size']=$_FILES[$html_name]['size']/1024;//单位是KB
        //按照cls_image类设置$_FILES[$html_name]数组以便调用cls_image类的函数
        $images_dir  = IMAGE_DIR;
        $data_dir    = DATA_DIR;
        /* 没有指定目录默认为根目录images */
        if (empty($save_dir))
        {
            /* 创建当月目录 */
            $save_dir = date('Ym');
            $save_dir = ROOT_PATH . $images_dir . '/' . $save_dir . '/';
        }
        else
        {
            /* 创建目录 */
            $save_dir = ROOT_PATH . $data_dir . '/' . $save_dir . '/';
            $save_name = $save_dir . $img_name; // 将图片定位到正确地址
        }
        
        $_FILES[$html_name]['tmp_name']=$save_name;
        
    }
    return $image_arr;
	
}
function copy_img($old_img, $new_img){//$upload是$_FILES[html_name]数组,$target是新文件路径
	include_once(ROOT_PATH . '/includes/cls_image.php');
    $image = new cls_image($_CFG['bgcolor']);
    $upload=array();
    $upload['error']=0;
    $upload['tmp_name']=$old_img;
    $dir_arr=explode('/',$new_img);
    array_pop($dir_arr);
    $new_img_dir=implode('/',$dir_arr);
    make_dir($new_img_dir);
    @$image->move_file($upload,$new_img);
    return true;
}
//------------------------------------------
function get_cmd_name($cmd_code){
	$cmd_name='';
	if(empty($cmd_code)){return $cmd_name;}
	switch ($cmd_code){
		case 'jiaoshui':
			$cmd_name="浇水";
			break;
		case 'chucao':
			$cmd_name="除草";
			break;
		case 'shifei':
			$cmd_name="施肥";
			break;
		case 'chuchong':
			$cmd_name="除虫";
			break;
		case 'caishou':
			$cmd_name="采收";
			break;
		case 'bozhong':
			$cmd_name="播种";
			break;
		case 'zhengdi':
			$cmd_name="整地";
			break;
		case 'paizhao':
			$cmd_name="拍照";
			break;
		case 'zhuangshi':
			$cmd_name="装饰";
			break;
		case 'carriage':
			$cmd_name="递送";
			break;
	}
	return $cmd_name;
	
}
//------------------------------------------
function get_index_in($data_list,$index){
	if(!empty($data_list)){
		foreach ($data_list as $key=>$val){
            $index_in.=$val[$index].",";
		}
	}
	$$index_in=substr($index_in,0,str_len($index_in)-1);
	return $index_in;
}
//------------------------------------------
function add_account($inf){
	$account_log_data=array();
	$user_account_data=array();
	
	$account_log_data['user_id']=$inf['user_id'];
	$account_log_data['user_money']=$inf['money'];
	$account_log_data['change_time']=$inf['now_time'];
	$account_log_data['change_desc']=$inf['admin_note'];
	switch ($inf['type']){//account_log表操作类型，0为充值，1为提现，2为管理员调节，99为其他类型',
		case 1:
			$account_log_data['change_type']=0;
			$user_account_data['process_type']=0;
			break;
		case 2:
			$account_log_data['change_type']=1;
			$user_account_data['process_type']=1;
			break;
		case 3:
			$account_log_data['change_type']=2;
			$user_account_data['process_type']=2;//2为管理员调节
			break;	
		case 4:
			$account_log_data['change_type']=3;//系统自动扣款
			$user_account_data['process_type']=3;//系统自动扣款
			break;	
	}
	
	$user_account_data['user_id']=$inf['user_id'];
	$user_account_data['admin_user']=$inf['admin_user'];//操作该笔交易的管理员的用户名'
	$user_account_data['amount']=$inf['money'];
	$user_account_data['add_time']=$inf['now_time'];
	$user_account_data['paid_time']=$inf['now_time'];
	$user_account_data['admin_note']=$inf['admin_note'];
	$user_account_data['user_note']=$inf['user_note'];
	$user_account_data['is_paid']=$inf['is_paid'];//是否已经付款，０，未付；１，已付',
	
	add_account_log_zc($account_log_data);
	add_user_account_zc($user_account_data);	
}
function add_account_log_zc($inf=array(),$inf_arr=array()){//添加用户帐户记录表
	
	if(empty($inf)&&empty($inf_arr)){return false;}

	if(!empty($inf)){
		unset($inf['log_id']);
		$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('account_log'), $inf, 'INSERT');
		$id=mysql_insert_id();
		return $id;
	}else{
		if(!empty($inf_arr)){
			$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('account_log'), $inf_arr, 'INSERT','','',true);
			return true;
		}
	}
   
}
function add_user_account_zc($inf=array(),$inf_arr=array()){//添加用户资金流动表
	
	if(empty($inf)&&empty($inf_arr)){return false;}

	if(!empty($inf)){
		unset($inf['id']);
		$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('user_account'), $inf, 'INSERT');
		$id=mysql_insert_id();
		return $id;
	}else{
		if(!empty($inf_arr)){
			$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('user_account'), $inf_arr, 'INSERT','','',true);
			return true;
		}
	}
   
}
//------------------------------------------
function edt_command($inf=array(),$inf_arr=array()){//编辑用户发起的指令表
	

	if(!empty($inf)){
		$inf_ok=$inf;
		unset($inf_ok['cmd_id']);
		$GLOBALS['db']->autoExecute($GLOBALS['zc']->table('command'), $inf_ok, 'UPDATE', 'cmd_id = ' . $inf['cmd_id']);
		return true;
	}else{
		if(!empty($inf_arr)){//更新记录的条件是$inf_arr[i]['where']
			//print_r($inf_arr);
			$GLOBALS['db']->autoExecute($GLOBALS['zc']->table('command'), $inf_arr, 'UPDATE','','',true);
			return true;
		}
	}
	
}
//------------------------------------------
function edt_users($inf=array(),$inf_arr=array()){//编辑用户表
	

	if(!empty($inf)){
		$inf_ok=$inf;
		unset($inf_ok['user_id']);
		$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('users'), $inf_ok, 'UPDATE', 'user_id = ' . $inf['user_id']);
		return true;
	}else{
		if(!empty($inf_arr)){//更新记录的条件是$inf_arr[i]['where']
			//print_r($inf_arr);
			$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('users'), $inf_arr, 'UPDATE','','',true);
			return true;
		}
	}
	
}
//------------------------------------------
function edt_recharge($inf=array(),$inf_arr=array()){//编辑用户充值表
	

	if(!empty($inf)){
		$inf_ok=$inf;
		unset($inf_ok['rch_id']);
		$GLOBALS['db']->autoExecute($GLOBALS['zc']->table('recharge'), $inf_ok, 'UPDATE', 'rch_id = ' . $inf['rch_id']);
		return true;
	}else{
		if(!empty($inf_arr)){//更新记录的条件是$inf_arr[i]['where']
			//print_r($inf_arr);
			$GLOBALS['db']->autoExecute($GLOBALS['zc']->table('recharge'), $inf_arr, 'UPDATE','','',true);
			return true;
		}
	}
	
}
//------------------------------------------
//------------------------------------------
?>